﻿//
// Created by LiuYou on 2021/4/25.
//

#include <iostream>
#include <cstring>

/**
 * @brief string类和C式string的接口测试。很重要！！！！
 */
int main() {
#ifdef HANZI
    char charArray[] = "你好";
    std::cout << "sizeof(charArray): " << sizeof(charArray) << std::endl;
    std::cout << "std::strlen(charArray): " << std::strlen(charArray) << std::endl << std::endl;
    char* ptrChar = "你好";
    std::cout << "sizeof(ptrChar): " << sizeof(ptrChar) << std::endl;
    std::cout << "std::strlen(ptrChar): " << std::strlen(ptrChar) << std::endl << std::endl;

    std::string string("你好");
    std::cout << "string.length(): " << string.length() << std::endl;
    std::cout << "string.size(): " << string.size() << std::endl;
    std::cout << "sizeof(string.c_str()): " << sizeof(string.c_str()) << std::endl;
    std::cout << "std::strlen(string.c_str()): " << std::strlen(string.c_str()) << std::endl << std::endl;

    string = "";
    std::cout << "string.length(): " << string.length() << std::endl;
    std::cout << "string.size(): " << string.size() << std::endl;
    std::cout << "sizeof(string.c_str()): " << sizeof(string.c_str()) << std::endl;
    std::cout << "std::strlen(string.c_str()): " << std::strlen(string.c_str()) << std::endl << std::endl;
#endif
#ifdef ZIMU
    char charArray[] = "nihao";
    std::cout << "sizeof(charArray): " << sizeof(charArray) << std::endl;
    std::cout << "std::strlen(charArray): " << std::strlen(charArray) << std::endl << std::endl;
    char* ptrChar = "nihao";
    std::cout << "sizeof(ptrChar): " << sizeof(ptrChar) << std::endl;
    std::cout << "std::strlen(ptrChar): " << std::strlen(ptrChar) << std::endl << std::endl;

    std::string string("nihao");
    std::cout << "string.length(): " << string.length() << std::endl;
    std::cout << "string.size(): " << string.size() << std::endl;
    std::cout << "sizeof(string.c_str()): " << sizeof(string.c_str()) << std::endl;
    std::cout << "std::strlen(string.c_str()): " << std::strlen(string.c_str()) << std::endl << std::endl;
    std::cout << "string.capacity(): " << string.capacity() << std::endl;

    string = "";
    std::cout << "string.length(): " << string.length() << std::endl;
    std::cout << "string.size(): " << string.size() << std::endl;
    std::cout << "sizeof(string.c_str()): " << sizeof(string.c_str()) << std::endl;
    std::cout << "std::strlen(string.c_str()): " << std::strlen(string.c_str()) << std::endl << std::endl;
    std::cout << "string.capacity(): " << string.capacity() << std::endl;

    // TODO 注意: 不论是汉字还是字母都是std::strlen()永远比sizeof()小1。
#endif

    char buffer[1024];
    std::memset(buffer, 0, sizeof(buffer));
    // TODO 注意: 有没有上面这一行代码(即memset)是不影响sizeof的输出结果的
    // 输出结果为: sizeof(buffer): 1024
    std::cout << "sizeof(buffer): " << sizeof(buffer) << std::endl;
    std::memset(buffer, 0, sizeof(buffer));
    // std::strlen(buffer): 0
    // TODO 注意: 有没有memset是影响std::strlen的结果的。因为没有初始化就是一堆乱码, 不知道字符串的长度的。
    //  但是memset后, std::strlen的结果就是0。
    std::cout << "std::strlen(buffer): " << std::strlen(buffer) << std::endl;

    std::strcpy(buffer, "你好");
    std::cout << buffer << std::endl;
    std::cout << "std::strlen(buffer)" << std::strlen(buffer) << std::endl;

    std::strcpy(buffer, "hello");
    std::cout << buffer << std::endl;
    std::cout << "std::strlen(buffer)" << std::strlen(buffer) << std::endl;


    std::strcpy(buffer, "L");
    std::cout << buffer << std::endl;
    std::cout << "std::strlen(buffer)" << std::strlen(buffer) << std::endl;

    std::strcpy(buffer, std::to_string(1).c_str());
    std::cout << buffer << std::endl;
    std::cout << "std::strlen(buffer)" << std::strlen(buffer) << std::endl;

    return 0;
}